package com.peas.platform.module.icims.base.approve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.common.core.enums.BaseEnum;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.domain.ApproveOrderNodeDomain;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderNodeDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveNodeType;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderNodeService;
import com.peas.platform.module.icims.base.auth.dto.UserDto;
import com.peas.platform.module.icims.base.auth.dto.UserRoleDto;
import com.peas.platform.module.icims.base.auth.service.UserRoleService;
import com.peas.platform.module.icims.base.auth.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;

@Service
public class ApproveOrderNodeServiceImpl extends BaseServiceImpl<ApproveOrderNodeDto> implements ApproveOrderNodeService {

    @Resource
    private ApproveOrderNodeDomain approveOrderNodeDomain;
    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;

    @Override
    public BaseDomain<ApproveOrderNodeDto, ? extends BasePo> getDomain() {
        return approveOrderNodeDomain;
    }

    @Override
    public List<ApproveOrderNodeDto> listByApproveOrderId(Long approveOrderId) {
        Assert.notNull(approveOrderId, "审批单ID不能为空");
        List<ApproveOrderNodeDto> aonds = this.listByField("approveOrderId", approveOrderId);
        if (CollUtil.isNotEmpty(aonds)) {
            aonds.sort(Comparator.comparing(ApproveOrderNodeDto::getApproveNodeNo));
        }
        return aonds;
    }

    @Override
    public void checkApproveOrderNode(List<ApproveOrderNodeDto> aonds) {
        Assert.notEmpty(aonds, "审批节点为空");
        aonds.forEach(dto -> {
            ApproveNodeType type = BaseEnum.key2Enum(ApproveNodeType.class, dto.getApproveNodeType());
            Assert.notNull(type, "节点类型不能为空");
            String str = "审批节点" + dto.getApproveNodeNo() + "[" + dto.getApproveNodeName() + "]";
            switch (type) {
                case user: {
                    String userIds = dto.getApproveNodeUserId();
                    StrUtil.split(userIds, ",").parallelStream().forEach(v -> {
                        Assert.notBlank(v, str + "，用户ID不能为空");
                        Long userId = Long.valueOf(v);
                        UserDto user = userService.getById(userId);
                        Assert.notNull(user, str + "，找不到用户");
                    });
                    break;
                }
                case org: {
                    String orgIds = dto.getApproveNodeOrgId();
                    StrUtil.split(orgIds, ",").parallelStream().forEach(v -> {
                        Assert.notBlank(v, str + "，组织架构ID不能为空");
                        Long orgId = Long.valueOf(v);
                        List<UserDto> list = userService.listByField("organizationId", orgId);
                        Assert.notEmpty(list, str + "，配置的组织架构下无用户");
                    });
                    break;
                }
                case role: {
                    String roleIds = dto.getApproveNodeRoleId();
                    StrUtil.split(roleIds, ",").parallelStream().forEach(v -> {
                        Assert.notBlank(v, str + "，角色ID不能为空");
                        Long roleId = Long.valueOf(v);
                        List<UserRoleDto> list = userRoleService.listByField("roleId", roleId);
                        Assert.notEmpty(list, str + "，配置的角色下无用户");
                    });
                    break;
                }
            }
        });
    }
}
